<?php
class Admin_TrainController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pTid = empty($p['t_id'])?'':Tool_Fnc::safe_string($p['t_id']);
        $pKeyword = empty($p['keyword'])?'':Tool_Fnc::safe_string($p['keyword']);
        $pTraindate = empty($p['train_date'])?'':Tool_Fnc::safe_string($p['train_date']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        
        $tDIMO = new DepartmentinfoModel;
        $tWMO = new WorkerModel;
        $tTMO = new TrainModel;
        $tAMO = new AdminModel;

        $tWhere = ' 1 '; 

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pKeyword)){
            $tWhere .= ' and w.name like \'%'.$pKeyword.'%\' or w.phone like \'%'.$pKeyword.'%\'';
        }
        if(!empty($pTraindate)){
            $tWhere .= ' and t.train_date like \'%'.$pTraindate.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and w.department  = '.$pDepartment;
        }

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            $tSql = 'select id,name from '.$tWMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tWMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        //所在部门
        $tDDatas = $tDIMO->field('num,introduce')->fList();
        //护工显示
        //$tSWDatas = $tWMO->field('id,introduce')->fList();
        
        $tSql = 'select count(*) c from '.$tTMO->table.' t left join '.$tWMO->table.' w on w.id = t.w_id left join '.$tDIMO->table.' di on di.num = w.department where '.$tWhere;
        $tCount = $tTMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select t.id id,w.name name,w.phone phone,di.introduce department,t.train_date train_date,t.train_type train_type,t.train_company train_company,t.train_name train_name,t.description description,t.is_certificate is_certificate,t.remarks remarks from '.$tTMO->table.' t left join '.$tWMO->table.' w on w.id = t.w_id left join '.$tDIMO->table.' di on di.num = w.department where '.$tWhere.' order by t.id desc limit '.$tPage->limit();
            $tDatas = $tTMO->query($tSql);
        }

        if(!empty($pTid)){//编辑页面显示
            //$tTData = $tTMO->field('*')->where('id = '.$pTid)->fRow();    
            $tSql = 'select t.*,w.department department,w.name name from '.$tTMO->table.' t left join '.$tWMO->table.' w on t.w_id = w.id where t.id = '.$pTid;
            $tTData = $tTMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tTData[0]);
        
        }
        $this->assign('pKeyword',$pKeyword);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('pTraindate',$pTraindate);
        $this->assign('tTData',$tTData[0]);
        $this->assign('tTDatas',$tTDatas);
        $this->assign('tDatas',$tDatas);
        $this->assign('tTraintype',$tTMO->tTraintype);
        $this->assign('tIscertificate',$tTMO->tIscertificate);
        $this->assign('tShow',$tPage->show()); 
        $this->assign('tSWDatas',$tSWDatas);
        $this->assign('tDDatas',$tDDatas);
    }
    public function addAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('护工ID不能为空'):Tool_Fnc::safe_string($p['supportworker']);
            $pTraintype = empty($p['train_type'])?'':Tool_Fnc::safe_string($p['train_type']);
            $pTraindate = empty($p['train_date'])?Tool_Fnc::ajaxMsg('培训时间不能为空'):Tool_Fnc::safe_string($p['train_date']);
            $pTraincompany = empty($p['train_company'])?Tool_Fnc::ajaxMsg('培训机构不能为空'):Tool_Fnc::safe_string($p['train_company']);
            $pTrainname = empty($p['train_name'])?Tool_Fnc::ajaxMsg('培训名称不能为空'):Tool_Fnc::safe_string($p['train_name']);
            $pIscertificate = empty($p['is_certificate'])?'':Tool_Fnc::safe_string($p['is_certificate']);
            $pDescription = empty($p['description'])?Tool_Fnc::ajaxMsg('培训内容不能为空'):Tool_Fnc::safe_string($p['description']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            $tTime = time();
            
            $tData = array(
                'w_id' => $pSupportworker,
                'train_type' => $pTraintype,
                'train_company' => $pTraincompany,
                'train_name' => $pTrainname,
                'is_certificate' => $pIscertificate,
                'description' => $pDescription,
                'remarks' => $pRemarks,
                'train_date' => $pTraindate,
                'created' => $tTime,    
        );
            $tTMO = new TrainModel;
            if(!$tId = $tTMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }  
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?Tool_Fnc::ajaxMsg('tId不能为空'):Tool_Fnc::safe_string($p['tId']);    
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('护工ID不能为空'):Tool_Fnc::safe_string($p['supportworker']);
            $pTraintype = empty($p['train_type'])?'':Tool_Fnc::safe_string($p['train_type']);
            $pTraindate = empty($p['train_date'])?Tool_Fnc::ajaxMsg('培训时间不能为空'):Tool_Fnc::safe_string($p['train_date']);
            $pTraincompany = empty($p['train_company'])?Tool_Fnc::ajaxMsg('培训机构不能为空'):Tool_Fnc::safe_string($p['train_company']);
            $pTrainname = empty($p['train_name'])?Tool_Fnc::ajaxMsg('培训名称不能为空'):Tool_Fnc::safe_string($p['train_name']);
            $pIscertificate = empty($p['is_certificate'])?'':Tool_Fnc::safe_string($p['is_certificate']);
            $pDescription = empty($p['description'])?Tool_Fnc::ajaxMsg('培训内容不能为空'):Tool_Fnc::safe_string($p['description']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            $tTime = time();
            
            $tData = array(
                'w_id' => $pSupportworker,
                'train_type' => $pTraintype,
                'train_company' => $pTraincompany,
                'train_name' => $pTrainname,
                'is_certificate' => $pIscertificate,
                'description' => $pDescription,
                'remarks' => $pRemarks,
                'train_date' => $pTraindate,
                'updated' => $tTime,
                'id' => $pTid,    
            );
            $tTMO = new TrainModel;
            if(!$tTMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }  
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);

        $tTMO = new TrainModel;
        $tSql = 'delete from '.$tTMO->table.' where id = '.$pId;
        if(!$tTMO->exec($tSql)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
